package com.fuwu8858.redismybatis.task;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.fuwu8858.redismybatis.Dao.RedisDao;
import com.fuwu8858.redismybatis.mapper.MemberMapper;

@Component
public class Scheduler {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private RedisDao redisDao;
	@Autowired
	MemberMapper memberMapper;
	ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);  
	@Scheduled(cron = "0 0/2 * * * ?") // 每2分钟执行一次
	public void statusCheck() {
		logger.info("每2分钟执行一次。统计5分钟内在线人数开始……");
		logger.warn("warn");
		String key = "indexMyNoReadCount:1:*";
		if (redisDao != null) {
			Set<String> set = redisDao.keys(key);
			// for (String string : set) {
			// logger.warn("{}={}", string, redisDao.get(string));
			// }

			logger.error("共计登录人数:{}", set.size());

		} else {
			logger.error("注入服务失败");
		}
		// statusTask.healthCheck();
		logger.info("每2分钟执行一次。统计5分钟内在线人数结束。");
	}

	// @Scheduled(fixedRate = 20000)
	// public void testTasks() {
	// logger.info("每20秒执行一次。开始……");
	// logger.warn("warn");
	// logger.error("error");
	// // statusTask.healthCheck();
	// logger.debug("每20秒执行一次。结束。");
	// }

	@Scheduled(cron = "0 0 0/1 * * ?") // 每天06:10固定执行一次)
	public void testTasks() {
		logger.info("每1小时固定执行一次。开始……");

		List<Map<String, Object>> list = memberMapper.select();
		for (Map<String, Object> map : list) {
			logger.warn("↓↓↓↓↓↓↓↓↓↓");
			for (Map.Entry<String, Object> entry : map.entrySet()) {
				logger.warn("{}={}", entry.getKey(), entry.getValue());
			}
			logger.warn("↑↑↑↑↑↑↑↑↑↑");
		}

		logger.info("每1小时固定执行一次。结束。");
	}
}
